/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.entityGenerator.entity.entityData;

import com.inspur.edp.caf.generator.baseInfo.AnnotationInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.property.InterfacePropertyGenerator;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import com.inspur.edp.cef.generator.common.MediatePropertyInfo;
import com.inspur.edp.cef.generator.entityGenerator.entity.EntityNodeGeneratorContext;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.Arrays;

public class DetailListPropGenerator extends InterfacePropertyGenerator {
  protected IGspCommonObject node;
  protected EntityNodeGeneratorContext nodeGenContext;
  protected String nodeCode;

  public DetailListPropGenerator(IGspCommonObject node, EntityNodeGeneratorContext nodeGenContext) {
    this.node = node;
    this.nodeGenContext = nodeGenContext;
    this.nodeCode = node.getCode();
  }

  public DetailListPropGenerator(IGspCommonObject node, EntityNodeGeneratorContext nodeGenContext, String nodeCode) {
    this.node = node;
    this.nodeGenContext = nodeGenContext;
    this.nodeCode = nodeCode;
  }

  @Override
  protected String getPropertyName() {
    return getPropName(this.nodeCode);
  }

  @Override
  protected TypeInfo getPropertyType() {
    //TODO: caf暂不支持, 主动加import
    nodeGenContext.getEntityGenContext().addImport(IEntityDataCollection.class.getName());
    return new TypeInfo(IEntityDataCollection.class);
  }

  @Override
  protected String getFieldName() {
    return null;
  }

  @Override
  protected boolean hasSetMethod() {
    return false;
  }

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
    TypeInfo t = getPropertyType();
    nodeGenContext.getDataGenContext().getPropertyInfos().put(getPropertyName(),
        new MediatePropertyInfo(getPropertyName()) {{
          setNode(node);
          setPropertyType(t);
        }}
    );
  }

  // region Static Members

  private static final String nameSuffix = "s";

  public static String getPropName(IGspCommonObject node) {
    return node.getCode() + nameSuffix;
  }

  public static String getPropName(String nodeCode) {
    return  nodeCode + nameSuffix;
  }
  // endregion

}
